package com.allenkerr.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.allenkerr.model.Todo;
import com.allenkerr.service.TodoService;
import com.allenkerr.vo.Result;

/**
 * Created by 小柯 on 2017/10/16.
 */
@RestController
@RequestMapping(value = "/admin_api/v1/todos", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TodoController {

  @Autowired
  private TodoService todoService;

  @GetMapping("")
  public Result<List<Todo>> listTodos() {
    return todoService.listTodos();
  }

  @PostMapping("")
  public Result<List<Todo>> addTodo(String task) {
    return todoService.addTodo(task);
  }

  /**
   * 更新操作
   * postman 中选择 x-www-form-urlencoded 进行操作
   *
   * @param id
   * @param task
   * @param done
   * @return Result
   */
  @PutMapping("/{id}")
  public Result changeTodo(@PathVariable Long id,
                           @RequestParam(required = false) String task,
                           @RequestParam(required = false) Boolean done) {
    Todo todo = new Todo(id, task, done);
    return todoService.changeTodo(todo);
  }

  @DeleteMapping("/{id}")
  public Result deleteTodo(@PathVariable Long id) {
    return todoService.deleteTodo(id);
  }

}
